package cn.com.anypay.manager.common.exception;

import cn.com.anypay.manager.common.enums.ResultCode;
import lombok.Getter;

/**
 * 业务异常类
 * 用于处理业务逻辑中的异常情况
 */
@Getter
public class BusinessException extends RuntimeException {

    private final ResultCode resultCode;
    private final String message;

    public BusinessException(ResultCode resultCode) {
        super(resultCode.getMessage());
        this.resultCode = resultCode;
        this.message = resultCode.getMessage();
    }

    public BusinessException(ResultCode resultCode, String message) {
        super(message);
        this.resultCode = resultCode;
        this.message = message;
    }

    public BusinessException(String message) {
        super(message);
        this.resultCode = ResultCode.BUSINESS_ERROR;
        this.message = message;
    }

    public BusinessException(ResultCode resultCode, String message, Throwable cause) {
        super(message, cause);
        this.resultCode = resultCode;
        this.message = message;
    }

    /**
     * 快速创建数据不存在异常
     */
    public static BusinessException dataNotFound(String resource) {
        return new BusinessException(ResultCode.DATA_NOT_FOUND, resource + "不存在");
    }

    /**
     * 快速创建数据已存在异常
     */
    public static BusinessException dataExists(String resource) {
        return new BusinessException(ResultCode.DATA_ALREADY_EXISTS, resource + "已存在");
    }

    /**
     * 快速创建操作不允许异常
     */
    public static BusinessException operationNotAllowed(String operation) {
        return new BusinessException(ResultCode.OPERATION_NOT_ALLOWED, operation + "操作不被允许");
    }

    /**
     * 快速创建参数校验异常
     */
    public static BusinessException validationError(String message) {
        return new BusinessException(ResultCode.VALIDATION_ERROR, message);
    }
}